<template>
    <div style="color: #000">
        <van-nav-bar
                left-arrow
                left-text="个人信息"
                right-text="编辑"
                @click-left="backRoute"
                @click-right="addInfo"
        >

        </van-nav-bar>

        <van-pull-refresh v-model="pullLoading" @refresh="onLoad">
            <van-list
                    v-model="loading"
                    :finished="finished"
                    :error.sync="error"
                    error-text="请求失败，点击重新加载"
                    @load="onLoad"
            >
            <van-cell-group>
                <van-cell center   >
                    <template #default>
                        <span style="display: inline-block;margin: 1.5rem auto">头像</span>
                        <van-image
                                round
                                style="margin-left:70%;position: absolute;top:1.2em"
                                width="3rem"
                                height="3rem"
                                :src="personalMsg.avatar">
                        </van-image>
                    </template>
                </van-cell>
                <van-cell title="姓名" :value="personalMsg.realname" />
                <van-cell title="别名" :value="personalMsg.username"/>
                <van-cell title="性别" :value="personalMsg.sex" />
                <van-cell title="生日" :value="personalMsg.birthday" />
            </van-cell-group>

            <van-cell-group class="cell-margin">
                <van-cell  title="对外信息显示" :value="getSubStringText(personalMsg.realname+'@'+personalMsg.orgCode,11)" value-class="cell-value" />
            </van-cell-group>

            <van-cell-group class="cell-margin">
                <van-cell title="所在部门" :value="personalMsg.orgCode" value-class="cell-value" />
                <van-cell title="工号" :value="personalMsg.workNo" />
                <van-cell title="状态" :value="personalMsg.status" />
            </van-cell-group>

            <van-cell-group class="cell-margin">
                <van-cell title="手机" :value="personalMsg.phone" />
                <!--<van-cell title="座机" :value="personalMsg.telephone" />-->
                <van-cell title="邮箱" :value="personalMsg.email" />
            </van-cell-group>

            <van-cell-group class="cell-margin">
                <van-cell title="职务" :value="personalMsg.post" />
                <van-cell title="身份" :value="personalMsg.identity" />
                <van-cell v-if="personalMsg.identity =='上级'" title="负责部门" :value="personalMsg.departIds" />

            </van-cell-group>
            </van-list>
        </van-pull-refresh>
    </div>
</template>

<script>
    import { getAction,getFileAccessHttpUrl } from '@/api'

    export default {
        name: "personalDetails",
        data(){
            return{
                businessCard:'对外名片',
                error:false,
                loading: false,
                pullLoading: false,
                finished:false,
                perList:[],
                personalMsg:{
                    avatar:'',
                    realname:'',
                    username:'',
                    sex:1,
                    birthday:new Date(),
                    orgCode:'',
                    workNo:'',
                    status:1,
                    phone:'',
                    telephone:'',
                    email:'',
                    post:'',
                    departIds:'',
                    identity:'',
                },
                userUrl:'/sys/user/queryById',
                positionUrl:'/sys/position/list',
                departUrl:'/sys/user/userDepartList',
                userId:'',
                id:''
            }
        },
        created(){
            let detail = this.$route.params
            this.personalMsg = detail
            // // this.personalMsg = Object.assign({},this.personalMsg,detail)
            // // this.$set(this.personalMsg,'email',detail.email)
            // this.personalMsg.email = detail.email
            // this.personalMsg.avatar = detail.avatar
            // this.personalMsg.realname = detail.realname
            // this.personalMsg.username= detail.username
            // this.personalMsg.sex = detail.sex
            // this.personalMsg.birthday = detail.birthday
            // this.personalMsg.departIds= detail.departIds
            // this.personalMsg.workNo= detail.workNo
            // this.personalMsg.phone= detail.phone
            // this.personalMsg.orgCode= detail.orgCode
            console.log('fff',this.$route.params)
            console.log('person',this.personalMsg.email)
        },
        methods: {
            backRoute() {
                this.$router.push({name: 'myHome'})
            },
            getSubStringText(text,len){
                if(!text || text.length==0){
                    return ''
                }
                if(text.length<len){
                    return text;
                }
                return text.substr(0,len)+"..."
            },
            onLoad(){
                this.loading = true
                this.pullLoading = true
                getAction(this.userUrl,{id:this.$store.getters.userid}).then(res=> {
                    this.loading = false
                    this.pullLoading = false
                    console.log("用户",res)
                    if (res.success) {
                        let perArr = res.result
                        if(perArr.avatar&&perArr.avatar.length >0)
                        this.personalMsg.avatar = getFileAccessHttpUrl(perArr.avatar)
                        this.personalMsg.realname = perArr.realname
                        this.personalMsg.username= perArr.username
                        this.personalMsg.post = perArr.post
                        this.personalMsg.sex = perArr.sex
                        this.personalMsg.birthday = perArr.birthday
                        this.personalMsg.departIds= perArr.departIds
                        this.personalMsg.workNo= perArr.workNo
                        this.personalMsg.phone= perArr.phone
                        this.personalMsg.telephone= perArr.telephone
                        this.personalMsg.email= perArr.email
                        this.personalMsg.post= perArr.post
                        this.personalMsg.identity= perArr.identity
                        this.personalMsg.status= perArr.status
                        this.personalMsg.orgCode= perArr.orgCode


                        if (this.personalMsg.sex === 1){
                            this.personalMsg.sex = '男'
                        }else{
                            this.personalMsg.sex = '女'
                        }
                        if (this.personalMsg.identity === 1){
                            this.personalMsg.identity = '普通成员'
                        }else{
                            this.personalMsg.identity = '上级'
                        }
                        if (this.personalMsg.status === 1){
                            this.personalMsg.status = '正常'
                        }else{
                            this.personalMsg.status = '冻结'
                        }

                        if( this.personalMsg.birthday == null){
                            this.personalMsg.birthday = '无'
                        }
                        if( this.personalMsg.telephone == null){
                            this.personalMsg.telephone = '无'
                        }

                    }
                    this.finished = true
                }).catch(e=>{
                    console.log("请求错误",e)
                    this.error = true;
                })
                this.loading = true
                getAction(this.departUrl,{userId:this.$store.getters.userid}).then(res=> {
                    this.loading = false
                    console.log("用户1",res)
                    if (res.success) {
                        for (let item of res.result){
                            this.personalMsg.orgCode = item.title
                            this.personalMsg.departIds = item.title
                            console.log('0909',item.title)
                        }
                    }
                    this.finished = true
                }).catch(e=>{
                    console.log("请求错误",e)
                    this.error = true;
                })
                this.loading = true
                getAction(this.positionUrl).then(res=> {
                    this.loading = false
                    console.log("用户2",res)
                    if (res.success) {
                        let postArr = res.result.records
                        for (let item of postArr ){
                            if (this.personalMsg.post == item.code){
                                this.personalMsg.post = item.name
                            }
                        }
                    }
                    this.finished = true
                }).catch(e=>{
                    console.log("请求错误",e)
                    this.error = true;
                })
            },
            addInfo(){
                let params = this.personalMsg
                this.$router.push({name: 'personalEdit',params:params})
                console.log("cs",params)
            },
        },

    }
</script>

<style scoped>
    .cell-margin{margin-top: 0.8rem}
    .cell-value{margin-left: -2rem}
</style>
